import axios from 'axios';
import Qs from 'qs';
import Tool from './utils.js';
const session = Tool.getSession();

// http request 拦截器
axios.interceptors.request.use(
    config => {
        if(config.url.indexOf("/tvucc-media") > -1 || config.url.indexOf("/ccp") > -1){
            if(config.method  === 'post' && config.data.constructor !== FormData && config.headers['Content-Type'] != "application/json;charset=UTF-8"){
                config.data = Qs.stringify(config.data);
            }
            if(config.headers['Content-Type'] != "application/json;charset=UTF-8"){  // json数据格式改为表单格式
                config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
            }
        }
        config.headers.Authorization = session;
        return config;
    },
    err => {
        return Promise.reject(err);
    });

axios.interceptors.response.use(
    response => {
        if(response.data.errorCode === '81000001') {
            // Bus.$emit("jumpLoginPage");
        }
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 402:
                case 401:
                    // window.location.href = "login.jsp";
                    // Bus.$emit("jumpLoginPage");
                    break;
            }
        }
        return Promise.reject(response);
    });
export default axios;